package com.xzj.admin.resource.sync.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xzj.admin.resource.sync.domain.entity.ResMessageSource;
import com.xzj.admin.resource.sync.domain.vo.SelectPortOption;
import com.xzj.admin.resource.sync.service.ResMessageSourceService;
import com.xzj.admin.resource.sync.service.ResScriptManageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.datasource.utils.XzjPageUtil;
import com.xzj.admin.resource.sync.domain.entity.ResFieldsReflection;
import com.xzj.admin.resource.sync.mapper.ResFieldsReflectionMapper;
import com.xzj.admin.resource.sync.service.ResFieldsReflectionService;

import java.util.List;
import java.util.Map;

/**
 * @program: xzj-admin
 * @ClassName: ResFieldsReflectionServiceImpl
 * @description: 源到目标字段映射标;(res_fields_reflection)表服务实现类
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Service
public class ResFieldsReflectionServiceImpl implements ResFieldsReflectionService{
    @Resource
    private ResFieldsReflectionMapper resFieldsReflectionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param reflectionId 主键
     * @return 实例对象
     */
    public ResFieldsReflection queryById(String reflectionId){
        return resFieldsReflectionMapper.queryById(reflectionId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    public PageResp<ResFieldsReflection> pageQuery(PageReq<ResFieldsReflection> pageReq){
        return XzjPageUtil.queryPage(pageReq, resFieldsReflection -> resFieldsReflectionMapper.pageQuery(resFieldsReflection));
    }

    /**
     * 新增数据
     *
     * @param resFieldsReflection 实例对象
     * @return 实例对象
     */
    public ResFieldsReflection insert(ResFieldsReflection resFieldsReflection){
        resFieldsReflectionMapper.insert(resFieldsReflection);
        return resFieldsReflection;
    }

    /**
     * 更新数据
     *
     * @param resFieldsReflection 实例对象
     * @return 实例对象
     */
    public ResFieldsReflection update(ResFieldsReflection resFieldsReflection){
        resFieldsReflectionMapper.update(resFieldsReflection);
        return resFieldsReflection;
    }

    /**
     * 通过主键删除数据
     *
     * @param reflectionId 主键
     * @return 是否成功
     */
    public boolean deleteById(String reflectionId){
        int total = resFieldsReflectionMapper.deleteById(reflectionId);
        return total > 0;
    }

    @Resource
    private ResScriptManageService resScriptManageService;

    @Resource
    private ResMessageSourceService resMessageSourceService;

    @Override
    public Map<String, List<SelectPortOption>> queryScriptBySourceId(String sourceId) {
        if(StrUtil.isEmpty(sourceId)){
            throw new RuntimeException("源表ID不能为空");
        }
        ResMessageSource resMessageSource = resMessageSourceService.queryById(sourceId);
        return resScriptManageService.queryAllByCloud(resMessageSource.getCloudOption());
    }
}